package de.g18.scavengerhunt.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import de.g18.scavengerhunt.AppApplicationContext;
import de.g18.scavengerhunt.types.ProfileViewAdapter;
import de.g18.scavengerhunt.types.User;
import de.g18.scavengerhunt.utils.GameUtils;
import de.g18.scavengerhunt.utils.WebserviceUtil;

/**
 * This is the layout of the view, to display other players in the session
 */
public class OtherProfilesActivity extends ListActivity {

    private WebserviceUtil      webserviceUtil = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        webserviceUtil = WebserviceUtil.getInstance();
        webserviceUtil.setContext(this);

        int ownUserId = AppApplicationContext.getUser().getId();

        List<String> allUserIDs = new ArrayList<String>();

        List<User> usersInSession = webserviceUtil.getActiveUsers(AppApplicationContext.getSession().getId());
        for (User currentUser : usersInSession) {
            if (currentUser.getId() != ownUserId) {
                allUserIDs.add(String.valueOf(currentUser.getId()));
            }
        }
        setListAdapter(new ProfileViewAdapter(this, allUserIDs.toArray(new String[allUserIDs.size()])));
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Give the ApplicationContext the current activity instance.
        AppApplicationContext.setActivity(this);
    }

    /**
     * This override makes sure, that the device back button only intents to load the GamesMapActivity.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            GameUtils.showActivity(new GameMapActivity());
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }
}
